<template>
  <div class="dynamic-tags">
    <el-tag
      v-for="(tag,index) in options"
      :key="tag"
      closable
      :disable-transitions="false"
      @close="handleClose(tag)"
    >
      {{ tag }}
    </el-tag>
    <el-input
      class="input-new-tag"
      v-if="inputVisible"
      v-model="inputValue"
      ref="saveTagInput"
      size="small"
      @keyup.enter.native="handleInputConfirm"
      @blur="handleInputConfirm"
    >
    </el-input>
    <el-button v-else class="button-new-tag" size="small" @click="showInput"
      >+</el-button
    >
  </div>
</template>

<script>
export default {
  name:'y-dynamic-tags',
  props:{
    options:{
      type:Array,
      default:[]
    }
  },
  data() {
    return {
      updateKey:false,
      dynamicTags: [],
      inputVisible: false,
      inputValue: "",
    };
  },
  methods: {
    handleClose(tag) {
      this.options.splice(this.options.indexOf(tag), 1);
      this.inputVisible = true
      this.inputVisible = false
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        
        if(!this.options.includes(inputValue)){
          this.options.push(inputValue);
        }else{
          $y.notifier.warning('包含相同标签！')
        }
      }
      this.inputVisible = false;
      this.inputValue = "";
    },
  },
};
</script>
<style lang="scss" scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>